package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import linter.linterjava.POJO.DO.ArticleReview;
import linter.linterjava.POJO.DO.ReviewReport;
import linter.linterjava.POJO.DTO.ReviewReportDTO;
import linter.linterjava.mapper.ArticleReviewMapper;
import linter.linterjava.service.ReviewReportService;
import linter.linterjava.mapper.ReviewReportMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static linter.linterjava.custom.CustomResultType.*;

/**
* @author 北煜
* @description 针对表【review_report(评论举报表)】的数据库操作Service实现
* @createDate 2024-07-07 12:16:15
*/
@Service
public class ReviewReportServiceImpl extends ServiceImpl<ReviewReportMapper, ReviewReport>
    implements ReviewReportService{

    @Resource
    ArticleReviewMapper articleReviewMapper;
    @Resource
    ReviewReportMapper reviewReportMapper;


    @Override
    public String reportReview(Long uuid, ReviewReportDTO reviewReportDTO) {
        Date date = new Date();
        ReviewReport reviewReport = new ReviewReport();
        ArticleReview isExistList = articleReviewMapper.selectById(reviewReportDTO.getReviewId());
        if (isExistList == null){ return COMMENT_NOT_EXIST; }
        if (reviewReportMapper.selectById(reviewReportDTO.getReviewId())!= null){
            Integer isReport = reviewReportMapper.selectById(reviewReportDTO.getReviewId()).getResultOfHanding();
            if ( isReport == null){ isReport = 0; }
            if (isReport == 1 && isReport == 2){ return COMMENT_REPORTED; }
        }
        reviewReport.setUserId(uuid);
        reviewReport.setReviewId(reviewReportDTO.getReviewId());
        reviewReport.setResultOfHanding(1);
        reviewReport.setCreateTime(date);
        reviewReport.setUpdataTime(date);
        int flag = reviewReportMapper.insert(reviewReport);
        ArticleReview articleReview = new ArticleReview();
        Integer reviewCommentNub = articleReviewMapper.selectById(reviewReportDTO.getReviewId()).getReviewCommentNub();
        articleReview.setReviewLikeNub(reviewCommentNub + 1);
        articleReview.setUpdataTime(date);
        flag += articleReviewMapper.update(articleReview, new QueryWrapper<ArticleReview>()
                .eq("id", reviewReportDTO.getReviewId()).eq("is_delete", 0));
        return flag == 2 ? REPORT_SUCCESS_REPORTED : COMMENT_REPORT_ERROR;
    }
}




